#include "Skybox.h"
#include "../PhysicsLogic.h"
#include <iostream> 

Skybox::Skybox(const std::string& skyboxFolder, float width, float height, float length) {
	std::string paths[texturesCount];
	paths[0] = skyboxFolder + "BACK.jpg";
	paths[1] = skyboxFolder + "RIGHT.jpg";
	paths[2] = skyboxFolder + "FRONT.jpg";
	paths[3] = skyboxFolder + "LEFT.jpg";
	paths[4] = skyboxFolder + "TOP.jpg";
	paths[5] = skyboxFolder + "DOWN.jpg";

	for(int i = 0; i < texturesCount; ++i) {
		this->textures[i].Load(paths[i].c_str(), true);
	}

	this->width = width;
	this->height = height;
	this->length = length;

	float px = - width  / 2;
	float py = - height / 2;
	float pz = - length / 2;

	this->renderlist = glGenLists(1);
	glNewList(this->renderlist, GL_COMPILE);
	glPushMatrix();
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	//glDepthMask(GL_FALSE);

	this->textures[0].Use();
	glBegin(GL_QUADS); //BACK
	glTexCoord2f(0, 1); glVertex3f(px + width, py		  , pz);
	glTexCoord2f(0, 0); glVertex3f(px + width, py + height, pz);
	glTexCoord2f(1, 0); glVertex3f(px		 , py + height, pz);
	glTexCoord2f(1, 1); glVertex3f(px		 , py	      , pz);
	glEnd();

	this->textures[1].Use();
	glBegin(GL_QUADS); //RIGHT
	glTexCoord2f(0, 1); glVertex3f(px + width, py		  , pz + length);
	glTexCoord2f(0, 0); glVertex3f(px + width, py + height, pz + length) ;
	glTexCoord2f(1, 0); glVertex3f(px + width, py + height, pz);
	glTexCoord2f(1, 1); glVertex3f(px + width, py         , pz);
	glEnd();

	this->textures[2].Use();
	glBegin(GL_QUADS); //FRONT
	glTexCoord2f(0, 1); glVertex3f(px		 , py		  , pz + length);
	glTexCoord2f(0, 0); glVertex3f(px		 , py + height, pz + length);
	glTexCoord2f(1, 0); glVertex3f(px + width, py + height, pz + length);
	glTexCoord2f(1, 1); glVertex3f(px + width, py		  , pz + length);
	glEnd();

	this->textures[3].Use();
	glBegin(GL_QUADS); //LEFT
	glTexCoord2f(0, 1); glVertex3f(px		, py		 , pz);
	glTexCoord2f(0, 0); glVertex3f(px		, py + height, pz);
	glTexCoord2f(1, 0); glVertex3f(px		, py + height, pz + length);
	glTexCoord2f(1, 1); glVertex3f(px		, py		 , pz + length);
	glEnd();

	this->textures[4].Use();
	glBegin(GL_QUADS); //TOP
	glTexCoord2f(0, 1); glVertex3f(px		 , py + height, pz + length);
	glTexCoord2f(0, 0); glVertex3f(px		 , py + height, pz);
	glTexCoord2f(1, 0); glVertex3f(px + width, py + height, pz);
	glTexCoord2f(1, 1); glVertex3f(px + width, py + height, pz + length); 
	glEnd();

	this->textures[5].Use();
	glBegin(GL_QUADS); //DOWN
	glTexCoord2f(0, 1); glVertex3f(px		 , py, pz); 
	glTexCoord2f(0, 0); glVertex3f(px		 , py, pz + length);
	glTexCoord2f(1, 0); glVertex3f(px + width, py, pz + length);
	glTexCoord2f(1, 1); glVertex3f(px + width, py, pz);
	glEnd();	

	//glDepthMask(GL_TRUE);
	glDisable(GL_TEXTURE_2D);
	glPopMatrix();
	glEndList();
}

Skybox::~Skybox() {

}

void Skybox::Draw() {
	glPushMatrix();
	const NxVec3& pos = PhysicsLogic::GetCamera().GetPosition();
	glTranslatef(pos.x, pos.y, pos.z);
	glCallList(renderlist);
	glPopMatrix();
}
